package com.example.ffepeg.ffe;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class SearchImg {
    public static void resizeImageWithSameQuality(String inputImagePath, String outputImagePath, int newWidth, int newHeight) {
        try {
            // 读取原始图像
            File inputFile = new File(inputImagePath);
            BufferedImage originalImage = ImageIO.read(inputFile);

            // 创建一个新的BufferedImage对象用于调整后的图像
            BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, originalImage.getType());

            // 使用Graphics2D进行绘制
            Graphics2D g = resizedImage.createGraphics();
            // 设置渲染提示以提高图像质量
            g.setRenderingHint(java.awt.RenderingHints.KEY_INTERPOLATION, java.awt.RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            // 绘制调整后的图像
            g.drawImage(originalImage, 0, 0, newWidth, newHeight, null);
            g.dispose();

            // 将调整后的图像写入文件
            ImageIO.write(resizedImage, "jpg", new File(outputImagePath));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void imgTest(String url){
        try {
            // 读取图像文件
            File file = new File(url);
            BufferedImage image = ImageIO.read(file);

            if (image != null) {
                // 获取图像的宽度和高度
                int width = image.getWidth();
                int height = image.getHeight();

                System.out.println("图像宽度: " + width);
                System.out.println("图像高度: " + height);
            } else {
                System.out.println("无法读取图像文件");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String img = "src/main/resources/file/logo/hungaguo.png";
        String out = "src/main/resources/out/output.png";
        // 测试代码
       // resizeImageWithSameQuality(img, out, 960, 1280);

        imgTest(img);
    }

}
